import cloneDeep from "lodash.clonedeep";

function convertMenusMaterial(nodes) {
  //遍历树  获取id数组
  for (var i in nodes) {
    nodes[i].title = nodes[i].name;
    nodes[i].icon = nodes[i].meta.icon;
    if (nodes[i].children) {
      nodes[i].group = nodes[i].path.slice(nodes[i].path.lastIndexOf("/"));
      //存在子节点就递归
      convertMenusMaterial(nodes[i].children);
    } else {
      nodes[i].to = nodes[i].path.slice(nodes[i].path.lastIndexOf("/") + 1);
    }
  }
}

export function convertRoutesMaterial(nodes) {
  if (!nodes) return null;
  nodes = cloneDeep(nodes);
  if (!Array.isArray(nodes)) {
    convertMenusMaterial([nodes]);
  } else {
    convertMenusMaterial(nodes);
  }
  return nodes;
}
